package com.woniuxy.courseweb.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.exception.ParameterValidException;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.coursedao.model.OrdinaryCourse;
import com.woniuxy.coursemodel.dto.PageDto;
import com.woniuxy.coursemodel.form.DayCourseForm;
import com.woniuxy.coursemodel.form.OneCourseForm;
import com.woniuxy.coursemodel.form.OrdinaryCoursePageForm;
import com.woniuxy.coursemodel.form.WeekCourseForm;
import com.woniuxy.coursemodel.param.DayCourseParam;
import com.woniuxy.coursemodel.param.OneCourseParam;
import com.woniuxy.coursemodel.param.OrdinaryCoursePageParam;
import com.woniuxy.coursemodel.param.WeekCourseParam;
import com.woniuxy.courseservice.service.OrdinaryCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/ordinaryCourse")
@Api(tags = "普通课程排课相关接口")
public class OrdinaryCourseController {

    @Resource
    private OrdinaryCourseService ordinaryCourseService;

    /**
     *
     * @param form 新增单个普通课程参数列表（产品id，开始时间，结束时间）
     * @return 无
     */
    @ApiOperation("新增单个普通课程方法")
    @PostMapping("/one.venue")
    public Result<?> one(@RequestBody @Validated OneCourseForm form){
        ordinaryCourseService.one(BeanUtil.toBean(form, OneCourseParam.class));
        return Result.success("新增单个普通课程成功");
    }

    /**
     *
     * @param ordinaryCourseId 课程id
     * @return 无
     */
    @ApiOperation("删除单个普通课程方法")
    @GetMapping("/del.venue")
    public Result<?> del(Integer ordinaryCourseId){
        ordinaryCourseService.del(ordinaryCourseId);
        return Result.success("删除单个普通课程成功");
    }

    /**
     *
     * @param form 排一天课程普通课程参数列表（产品id集合,排课的日期）
     * @return 无
     */
    @ApiOperation("排列一整天普通课程方法")
    @PostMapping("/day.venue")
    public Result<?> day(@RequestBody @Validated DayCourseForm form){
        ordinaryCourseService.day(BeanUtil.toBean(form, DayCourseParam.class));
        return Result.success("排列一整天普通课程成功");
    }

    /**
     *
     * @param form 排一周课程参数列表（开始日期，结束日期）
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("随机排列一周普通课程方法")
    @PostMapping("/week.venue")
    public Result<?> week(@RequestBody @Validated WeekCourseForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        WeekCourseParam param = BeanUtil.toBean(form, WeekCourseParam.class);
        param.setVenueId(venueId);
        ordinaryCourseService.week(param);
        return Result.success("随机排列一周普通课程成功");
    }

    @ApiOperation("查看普通课程方法")
    @PostMapping("/byPage.venue")
    public Result<PageInfo<PageDto>> byPage(@RequestBody @Validated OrdinaryCoursePageForm form, BindingResult br , @RequestHeader String token){
        if (br.hasErrors()) throw new ParameterValidException(500,"参数输入有误");
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        OrdinaryCoursePageParam param = BeanUtil.toBean(form, OrdinaryCoursePageParam.class);
        param.setVenueId(venueId);
        PageInfo<PageDto> pageInfo = ordinaryCourseService.byPage(param);
        return Result.success("查看普通课程方法成功",pageInfo);
    }

    @ApiOperation("通过普通课程ids删除课程方法")
    @PostMapping("/delByIds.venue")
    public Result<?> delByIds(@RequestBody List<Integer> ordinaryCourseIds,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        ordinaryCourseService.delByIds(ordinaryCourseIds,venueId);
        return Result.success("通过普通课程ids删除课程成功");
    }
}

